package com.xy.nio;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class NIOFileChannel03 {
    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("1.txt");
        FileChannel fileChannel01 = fis.getChannel();

        FileOutputStream fos = new FileOutputStream("2.txt");
        FileChannel fileChannel02 = fos.getChannel();
        ByteBuffer bb = ByteBuffer.allocate(512);
        while (true) {
            //这个clear很重要,没有会导致循环不会结束
            bb.clear();
            int read = fileChannel01.read(bb);
            System.out.println(read);
            if (read == -1) {
                break;
            }
            bb.flip();
            fileChannel02.write(bb);
        }
        fis.close();
        fos.close();
    }
}
